package hupu


import (
	"os"
	"time"
	"log"
	"strings"
	"strconv"
	"github.com/PuerkitoBio/goquery"
)


//测试写文件
func Foo(){

	userFile := "test.txt"
	fout,err := os.OpenFile(userFile,os.O_CREATE|os.O_APPEND|os.O_RDWR,0660)
	defer fout.Close()

	if err != nil{
		panic(err)
		return
	}

	fout.WriteString(time.Now().String() + "\r\n")


	// doc,err := goquery.NewDocument(HUPU_NBA_URL)
	// if err != nil{
	// 	log.Fatal(err)
	// }
	// sel := doc.Find(".players_table").Find("tr")
	// fmt.Println(sel.Size())
	// fmt.Println(sel.Eq(2).Html())
	// for i := range sel.Nodes {
	// 	tr := sel.Eq(i)
	// 	// title := tr.Find("tr")
	// 	// fmt.Println(title.Size())
	// 	fmt.Println(tr.Html())
	// }

	// sel := doc.Find(".linglei")
	
	// fmt.Println(sel.Size())
	// fmt.Println(sel.Text())
	
	// fmt.Println(sel.Eq(1).Text())

	// fmt.Println(sel.First().Html())
	// fmt.Println(sel.Last().Text())

	// fmt.Println(HUPU_NBA_URL)
}


//初始球队抓取
func InitTeam(){
	
	doc,err := goquery.NewDocument(HUPU_NBA_TEAMS_URL)
	if err != nil{
		log.Fatal(err)
	}
	divs := doc.Find(".team")	 //取得6个赛区
	
	a_list := divs.Eq(0).Find("a")
	for i := range a_list.Nodes{
		_url,_ := a_list.Eq(i).Attr("href")
		_img,_ := a_list.Eq(i).Find("img").Attr("src")
		_name := a_list.Eq(i).Find("h2").Text()
		_str := a_list.Eq(i).Find("p").Text()
		_str = strings.Replace(_str, "(", "",-1)
		_str = strings.Replace(_str, "负)", "",-1)
		_s := strings.Split(_str, "胜")
		_win,_:= strconv.Atoi(_s[0])
		_lost,_ := strconv.Atoi(_s[1])
		_area := "西部"
		_zone := "西南赛区"
		
		InsertTeam(_name,_area,_zone,_url,_img,_win,_lost)

	}

	a_list = divs.Eq(1).Find("a")
	for i := range a_list.Nodes{
		_url,_ := a_list.Eq(i).Attr("href")
		_img,_ := a_list.Eq(i).Find("img").Attr("src")
		_name := a_list.Eq(i).Find("h2").Text()
		_str := a_list.Eq(i).Find("p").Text()
		_str = strings.Replace(_str, "(", "",-1)
		_str = strings.Replace(_str, "负)", "",-1)
		_s := strings.Split(_str, "胜")
		_win,_:= strconv.Atoi(_s[0])
		_lost,_ := strconv.Atoi(_s[1])
		_area := "西部"
		_zone := "太平洋赛区"
		
		InsertTeam(_name,_area,_zone,_url,_img,_win,_lost)

	}


	a_list = divs.Eq(2).Find("a")
	for i := range a_list.Nodes{
		_url,_ := a_list.Eq(i).Attr("href")
		_img,_ := a_list.Eq(i).Find("img").Attr("src")
		_name := a_list.Eq(i).Find("h2").Text()
		_str := a_list.Eq(i).Find("p").Text()
		_str = strings.Replace(_str, "(", "",-1)
		_str = strings.Replace(_str, "负)", "",-1)
		_s := strings.Split(_str, "胜")
		_win,_:= strconv.Atoi(_s[0])
		_lost,_ := strconv.Atoi(_s[1])
		_area := "西部"
		_zone := "西北赛区"
		
		InsertTeam(_name,_area,_zone,_url,_img,_win,_lost)

	}

	
	a_list = divs.Eq(3).Find("a")
	for i := range a_list.Nodes{
		_url,_ := a_list.Eq(i).Attr("href")
		_img,_ := a_list.Eq(i).Find("img").Attr("src")
		_name := a_list.Eq(i).Find("h2").Text()
		_str := a_list.Eq(i).Find("p").Text()
		_str = strings.Replace(_str, "(", "",-1)
		_str = strings.Replace(_str, "负)", "",-1)
		_s := strings.Split(_str, "胜")
		_win,_:= strconv.Atoi(_s[0])
		_lost,_ := strconv.Atoi(_s[1])
		_area := "东部"
		_zone := "大西洋赛区"
		
		InsertTeam(_name,_area,_zone,_url,_img,_win,_lost)

	}

	a_list = divs.Eq(4).Find("a")
	for i := range a_list.Nodes{
		_url,_ := a_list.Eq(i).Attr("href")
		_img,_ := a_list.Eq(i).Find("img").Attr("src")
		_name := a_list.Eq(i).Find("h2").Text()
		_str := a_list.Eq(i).Find("p").Text()
		_str = strings.Replace(_str, "(", "",-1)
		_str = strings.Replace(_str, "负)", "",-1)
		_s := strings.Split(_str, "胜")
		_win,_:= strconv.Atoi(_s[0])
		_lost,_ := strconv.Atoi(_s[1])
		_area := "东部"
		_zone := "东南赛区"
		
		InsertTeam(_name,_area,_zone,_url,_img,_win,_lost)
	}

	a_list = divs.Eq(5).Find("a")
	for i := range a_list.Nodes{
		_url,_ := a_list.Eq(i).Attr("href")
		_img,_ := a_list.Eq(i).Find("img").Attr("src")
		_name := a_list.Eq(i).Find("h2").Text()
		_str := a_list.Eq(i).Find("p").Text()
		_str = strings.Replace(_str, "(", "",-1)
		_str = strings.Replace(_str, "负)", "",-1)
		_s := strings.Split(_str, "胜")
		_win,_:= strconv.Atoi(_s[0])
		_lost,_ := strconv.Atoi(_s[1])
		_area := "东部"
		_zone := "中部赛区"
		
		InsertTeam(_name,_area,_zone,_url,_img,_win,_lost)
	}

	
	
}

